package yxy.game.pm2.module;

import java.util.*;

import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.banana.socket.NetworkElement;
import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.bean.ChatBean;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.Point;
import yxy.game.pm2.bean.area.ChatChannelType;
import yxy.game.pm2.bean.currency.CurrencyGroup;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.message.*;
import yxy.game.pm2.module.player.RankPlayerPower;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.therionHunting.TherionModule;

public class NotificationHandle {

    public void notifyPointByCurrency(String playerCode, Collection<Currency> currencies) {
        Set<CurrencyType> types = new HashSet<>();
        for (Currency currency : currencies) {
            types.add(currency.getType());
        }
        notifyPoint(playerCode, types);
    }

    public void notifyPointByCurrency(String playerCode, Currency... currencies) {
        notifyPointByCurrency(playerCode, Arrays.asList(currencies));

    }

    public void notifyPoint(String playerCode, CurrencyType... types) {
        if (types == null) {
            return;
        }
        notifyPoint(playerCode, Arrays.asList(types));
    }

    public void notifyPoint(String playerCode, Collection<CurrencyType> types) {
        if (types == null) {
            return;
        }
        for (CurrencyType type : types) {
            notifyPoint(playerCode, type);
        }
    }

    public void notifyPoint(String playerCode, CurrencyType type) {
        if (!type.getGroup().equals(CurrencyGroup.POINT)) {
            return;
        }

        long value = Point.get(playerCode, type, false);

        NotificationMessage.CurrencyNotification.Builder notify = NotificationMessage.CurrencyNotification.newBuilder();
        PackageMessage.Currency.Builder currency = PackageMessage.Currency.newBuilder();
        currency.setType(type.getNumber());
        currency.setNumber(value);
        notify.setCurrency(currency);
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    /**
     * 推送战力
     */
    public void notifyBattlePower(String playerCode, long power) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        try {
            if (playerBean.tryLock()) {
                if (power == playerBean.getPower()) {
                    return;
                }
                playerBean.setPower(power);
                playerBean.set();

                //异步处理
                {
                    Runnable runnable = () -> {
                        TaskHandle.handle(playerCode, TaskType.指标型_玩家_战力_达到, power);
                        // 排行榜
                        RankPlayerPower.getRank(playerCode).put(playerCode, power);
                    };
                    ThreadPoolManager.getInstance().execute(runnable);
                }


                NotificationMessage.BattlePowerNotification.Builder notify = NotificationMessage.BattlePowerNotification.newBuilder();
                notify.setPower(power);
                MessageEmitterHandler.getInstance().send(playerCode, notify.build());

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            playerBean.unlock();
        }
    }

    /**
     * 推送等级
     */
    public void notifyPlayerLevel(String playerCode, int level, long exp) {
        NotificationMessage.UpgrageNotification.Builder notify = NotificationMessage.UpgrageNotification.newBuilder();
        notify.setLevel(level);
        notify.setExp(exp);
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    /**
     * 推送内部系统消息
     */
    public void notifyChatSystem(String text, String playerCode) {
        notifyChatInner(ChatChannelType.CHAT_SYS, 0, text, playerCode);
    }

    /**
     * 推送内部滚屏消息
     */
    public void notifyChatScroll(String text, String playerCode) {
        notifyChatInner(ChatChannelType.CHAT_SCROLL, 0, text, playerCode);
    }

    /**
     * 推送内部消息
     */
    public void notifyChatInner(ChatChannelType channelType, int areaId, String text, String playerCode) {
        int type = channelType.getNumber();
        if (type == 4) {
            type = 99;
        }
        ChatMessage.ChatChannel channel = ChatMessage.ChatChannel.forNumber(type);
        ChatMessage.ChatSendInnerRequest.Builder builder = ChatMessage.ChatSendInnerRequest.newBuilder();

        builder.setAreaId(areaId);
        builder.setChannel(channel);
        builder.setText(text);
        builder.setPlayerCode(playerCode);

        ChatBean chatBean = ChatBean.get(RedisManager.getInstance().getConnection(RedisType.REDIS_USER));
        long target = NetworkElement.value(chatBean.getSystem(), chatBean.getCluster(), 0);

        MessageSource source = new MessageSource(chatBean.getInnertoken(), target, 0);
        MessageEmitterHandler.getInstance().send(source, builder.build());
    }

    public void notifyChatInner(String playerCode, GameString gameString) {
        ChatChannelType channelType = ChatChannelType.forNumber(gameString.getChattype());
        if (channelType == null) {
            return;
        }
        int areaId = 0;
        if (channelType.equals(ChatChannelType.CHAT_AREA)) {
            PlayerBean playerBean = PlayerBean.get(playerCode);
            areaId = playerBean.getServerId();
        }

        notifyChatInner(channelType, areaId, gameString.getText(), playerCode);
    }

    /**
     * 推送vip等级
     */
    public void notifyPlayerVipLevel(String playerCode, int viplevel) {
        NotificationMessage.VipUpgrageNotification.Builder notify = NotificationMessage.VipUpgrageNotification.newBuilder();
        notify.setViplevel(viplevel);
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    /**
     * 推送活动开关与否
     **/
    public void notifyActiveOpenAndClose(String playerCode, int acttype, int state) {
        NotificationMessage.ActiveOpenAndCloseNotification.Builder notify = NotificationMessage.ActiveOpenAndCloseNotification.newBuilder();
        NotificationMessage.ActOpen.Builder actopen = NotificationMessage.ActOpen.newBuilder();
        actopen.setActtype(acttype);
        actopen.setState(state);
        notify.setActopens(actopen.build());
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    /**
     * 推送世界等级
     */
    public void notifyWorldLevel(String playerCode, int level) {
        NotificationMessage.WorldLevelNotification.Builder notify = NotificationMessage.WorldLevelNotification.newBuilder();
        notify.setWorldlevel(level);
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    /**
     * 推送称号
     */
    public void notifyPlayerTitle(PlayerBean oldBean) {
        NotificationMessage.PlayerTitleNotification.Builder notify = NotificationMessage.PlayerTitleNotification.newBuilder();
        notify.setTitle(oldBean.getTitle());
        MessageEmitterHandler.getInstance().send(oldBean.getCode(), notify.build());
    }

    /**
     * 推送神兽复活
     *
     * @param therionModule
     */
    public void notifyTherionResurgence(String playerCode, TherionModule therionModule) {
        NotificationMessage.TherionResurgenceNotification.Builder notify = NotificationMessage.TherionResurgenceNotification.newBuilder();
        notify.setTherionName(therionModule.getName());
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

    /**
     * 推送神兽玩家被击败  TherionDefeatNotification
     *
     * @param therionName
     * @param gameType
     */
    public void notifyTherionDefeat(String playerCode, String opposCode, String therionName, String playerName, int gameType) {
        NotificationMessage.TherionDefeatNotification.Builder notify = NotificationMessage.TherionDefeatNotification.newBuilder();
        notify.setTherionName(therionName);
        notify.setOpposCode(playerCode);
        notify.setOpposName(playerName);
        notify.setGameType(gameType);
        MessageEmitterHandler.getInstance().send(opposCode, notify.build());
    }


    /**
     * 推送单个武将红点信息  HeroEquipTipNotification
     */
    public void HeroEquipTipNotification(String playerCode, String code, Collection<Integer> tips) {
        NotificationMessage.HeroEquipTipNotification.Builder notify = NotificationMessage.HeroEquipTipNotification.newBuilder();
        notify.addAllTips(tips);
        notify.setCode(code);
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }


    /**
     * 在线玩家匹配结果推送  MatchOnlineResultNotification
     *
     * @param playerCode
     * @param builder
     * @param number
     * @param reward
     * @param msg
     */
    public void MatchOnlineResultNotification(String playerCode, BattleMessage.BattleExecuteResponse.Builder builder, int number, Collection<PackageMessage.Item> reward, MatchOnlineMessage.MatchPlayerOnlineInfoMsg.Builder msg) {
        NotificationMessage.MatchOnlineResultNotification.Builder notify = NotificationMessage.MatchOnlineResultNotification.newBuilder();
        notify.setBattleResp(builder);
        notify.setBattleResult(number);
        if (reward != null) notify.addAllRewards(reward);
        notify.setMsg(msg);
        MessageEmitterHandler.getInstance().send(playerCode, notify.build());
    }

}
